<?php

namespace App\Http\Controllers\background;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Mysql\{XnMechantProfit, XnMerchant, XnOrder, XnOrderBill, XnTerraceProfit};
use App\Models\Admin\{InterfaceConfiguration, SupplierBalanceChangeRecord};
use Illuminate\Http\Request;

class ImportInfoController extends Controller
{
    public function accountBill(Request $request){
        $goods_id = $request -> post('goods_id','');
        $types = $request -> post('types','');
        $start_time = $request -> post('start_time','');
        $end_time = $request -> post('end_time','');
        $bookkeeping_type = $request -> post('bookkeeping_type','');
        $business_type = $request -> post('business_type','');
        $Supplier_status = $request -> post('Supplier_status','');
        $order_no = $request -> post('order_no','');
        $recharge_status = $request -> post('recharge_status','');
        // 接收时间条件
        if(!empty($start_time) && !empty($end_time)){
            $condition[] = ['ctime','>=',$start_time];
            $condition[] = ['ctime','<=',$end_time];
        }
        if(!empty($goods_id)){
            $condition[] = ['goods_id','=',$goods_id];
        }
        // 接收记账类型条件
        if(!empty($bookkeeping_type)){
            $condition[] = ['bookkeeping_type','=',$bookkeeping_type];
        }
        // 接收业务类型条件
        if(!empty($business_type)){
            $condition[] = ['business_type','=',$business_type];
        }
        // 接收商品归属
        if(!empty($Supplier_status)){
            $condition[] = ['goods_belong','=',$Supplier_status];
        }
        // 接收业务订单号
        if(!empty($order_no)){
            $condition[] = ['order_sn','=',$order_no];
        }
        // 接收充值状态
        if(!empty($recharge_status)){
            $condition[] = ['recharge_status','=',$recharge_status];
        }
        $condition[] = ['order_type','=',$types];
        $res = XnOrderBill::where($condition)
            -> orderBy('id','desc')
            -> get()
            ->toArray();
        if($res){
            foreach($res as $k => $v){
                $res[$k]['goods_belong'] = InterfaceConfiguration::where('id',$v['goods_belong']) -> value('title');
            }
        }
        return successly($res);
    }

    public function balanceBill(Request $request){
        $before_atime = $request-> post('before_atime','');
        $after_atime = $request-> post('after_atime','');
        $order_no = $request-> post('order_no','');
        $supplier_id = $request -> post('supplier_id','');
        $type = $request -> post('type','');
        if(empty($supplier_id)){
            return fails('请传参');
        }
        $condition = [];
        if(!empty($order_no)){
            $condition[] = ['order_no','=',$order_no];
        }
        if(!empty($before_atime) && !empty($after_atime)){
            $condition[] = ['create_time','>=',$before_atime];
            $condition[] = ['create_time','<=',$after_atime];
        }
        if(!empty($type)){
            $condition[] = ['type','=',$type];
        }
        $infos = SupplierBalanceChangeRecord::where($condition)
            -> select('create_time','order_no','type','before_money','money','after_money')
            -> orderBy('create_time','desc')
            -> get()
            -> toArray();
        return successly($infos);
    }

    public function mechantlist(Request $request){
        $mechant_name = $request->post('mechant_name','');
        $phone = $request->post('phone','');
        $mechant_number = $request->post('mechant_number','');
        $arr = [];
        if(!empty($mechant_name)){
            $arr[] = ['nickname','like','%'.$mechant_name.'%'];
        }
        if(!empty($phone)){
            $arr[] = ['phone','=',$phone];
        }

        if(!empty($mechant_number)){
            $arr[] = ['merchant_number','=',$mechant_number];
        }
        $res =  XnMerchant::where($arr) -> orderBy('time','desc') -> get() -> toArray();
        foreach($res as $k => $v){
            $res[$k]['balance'] = round($v['balance'] - $v['deduction_balance'],4);
        }
        return successly($res);
    }

    public function orderbilllist(Request $request){
        $b_type = $request-> post('b_type');
        $before_atime = $request-> post('before_atime','');
        $after_atime = $request-> post('after_atime','');
        $order_no = $request-> post('order_no','');
        $order_type = $request-> post('order_type','');
        $supplier_status = $request-> post('supplier_status','');
        $mecahnt_number = $request-> post('mechant_number','');
        if(!empty($supplier_status)){
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        if(!empty($order_no)){
            $condition[] = ['order_sn','=',$order_no];
        }
        if(!empty($mecahnt_number)){
            $condition[] =  ['merchant_number','=',$mecahnt_number];
        }
        if(!empty($b_type)){
            $condition[] = ['bookkeeping_type','=',$b_type];
        }
        if(!empty($before_atime) && !empty($after_atime)){
            $condition[] = ['ctime','>=',$before_atime];
            $condition[] = ['ctime','<=',$after_atime];
        }

        $condition[]= ['order_type','=',$order_type];
        $list = XnOrderBill::where($condition) -> orderBy('ctime','desc') ->get() -> toArray();
        if(!$list){
            return  fails('暂无数据');
        }
        return successly($list);
    }

    public function profitlist(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $type = $request->post('order_type','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        $condition[] = ['order_type','=',$type];
        $condition[] = ['recharge_status','=',1];

        $list = XnTerraceProfit::orderBy('ctime','desc')->get() -> toArray();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['allmoney'] = $this -> getsummoneys($condition,$v['supplier_status'],1);
                $list[$k]['allcount'] = $this -> getsummoneys($condition,$v['supplier_status'],2);
            }
        }
        return successly($list);
    }
    public function getsummoneys($condition,$supplier_status,$type){
        $arr = [];
        if($supplier_status == 10000){
            $arr = [1,2];
        }else{
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        $money = XnOrder::where($condition)
            -> whereNotIn('goods_belong',$arr)
            -> select(DB::raw('SUM(all_money-unitPrice*goods_num) as allprofit,COUNT(*) as allcount'))
            -> first()->toArray();

        if($type == 1){
            return round($money['allprofit'],4);
        }else if($type == 2){
            return $money['allcount'];
        }
    }

    public function profitdetailed(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $type = $request->post('order_type','');
        $supplier_status = $request->post('Supplier_status','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        $condition[] = ['order_type','=',$type];
        $condition[] = ['recharge_status','=',1];
        $arr = [];
        if($supplier_status == 10000){
            $arr = [1,2];
        }else{
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        $list = XnOrder::where($condition)
            -> whereNotIn('goods_belong',$arr)
            -> select(DB::raw('recharge_time,goods_belong,mechant_name,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            -> orderBy('recharge_time','desc')
            -> get()
            -> toArray();
        foreach($list as $k =>$v){
            $list[$k]['goods_belong'] = InterfaceConfiguration::find($v['goods_belong']) -> title;
        }
        return successly($list);
    }

    public function interfacedetail(Request $request){
        $start_time = $request -> post('start_time','');
        $end_time = $request -> post('end_time','');
        $goods_name = $request -> post('goods_name','');
        $order_sn = $request -> post('order_sn','');
        $order_status = $request -> post('order_status','');
        $s_id = $request -> post('sid','');
        if(empty($s_id)){
            return  fails('请传参数');
        }
        $condition[] = ['goods_belong','=',$s_id];
        $condition[] = ['recharge_time','>=',$start_time];
        $condition[] = ['recharge_time','<=',$end_time];
        if(!empty($goods_name)){
            $condition[] = ['goods_name','like','%'.$goods_name.'%'];
        }
        if(!empty($order_sn)){
            $condition[] = ['order_sn','=',$order_sn];
        }
        if(!empty($order_status)){
            $condition[] = ['recharge_status','=',$order_status];
        }
        $info =  XnOrder::where($condition)
            -> select('recharge_time','mechant_name','goods_name','order_sn','unitPrice','account','goods_num')
            -> orderBy('recharge_time','desc')
            -> get()
            -> toArray();
        return successly($info);
    }

    public  function dayProfit(Request $request)
    {
        $starttime = $request->post('starttime', '');
        $endtime = $request->post('endtime', '');
        $supplier_name = $request->post('mechant_name', '');
        $mechant_number = $request->post('mechant_number', '');
        $order_num = $request->post('order_num', '');
        $order_type = $request->post('order_type', '');
        $condition = [];
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time', '>=', $starttime];
            $condition[] = ['recharge_time', '<=', $endtime];
        }
        if (!empty($supplier_name)) {
            $condition[] = ['mechant_name', 'like', '%' . $supplier_name . '%'];
        }
        if (!empty($mechant_number)) {
            $condition[] = ['merchant_number', '=', $mechant_number];

        }
        if (!empty($order_num)) {
            $condition[] = ['order_sn', '=', $order_num];
        }
        $condition[] = ['recharge_status', '=', 1];
        $condition[] = ['order_type', '=', $order_type];

        $list = XnOrder::where($condition)
            ->select(DB::raw('recharge_time,goods_belong,mechant_name,merchant_number,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            ->orderBy('recharge_time', 'desc')
            ->get()
            ->toArray();
        return successly($list);
    }

    public function monthprofitlist(Request $request){
        $year =$request->post('year','');
        $month = $request->post('month','');
        $mechant_number = $request->post('mechant_number','');
        $condition[] = ['year','=',$year];
        if(!empty($mechant_number)){
            $condition[] = ['mechant_number','=',$mechant_number];
        }
        $select ='mechant_number,month,SUM(income_number) as i_number,SUM(income_money) as i_money,SUM(profit) as allProfit';
        if(!empty($month)){
            $condition[] = ['month','=',$month];
            $list = XnMechantProfit::where($condition)
                -> groupBy('mechant_number')
                -> select($select)
                -> get()
                -> toArray();
            if($list){
                foreach($list as $k => $v){
                    $list[$k]['mechant_name'] = XnMerchant::where('merchant_number','=',$v['mechant_number']) -> value('nickname');
                }
            }

        }else{
            $list = XnMechantProfit::where($condition)
                -> groupBy('mechant_number','month')
                -> selectRaw($select)
                -> get()
                -> toArray();
            if($list){
                foreach($list as $k => $v){
                    $list[$k]['mechant_name'] = XnMerchant::where('merchant_number','=',$v['mechant_number']) -> value('nickname');
                }
            }

        }
        return  successly($list);
    }
    public function dayprofitdetail(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $mechant_number = $request->post('mechant_number','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        if(!empty($mechant_number)){
            $condition[] = ['merchant_number','=',$mechant_number];
        }

        $condition[] = ['recharge_status','=',1];
        $list = XnOrder::where($condition)
            -> select(DB::raw('recharge_time,goods_belong,mechant_name,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            -> orderBy('recharge_time','desc')
            -> get()
            -> toArray();
        return successly($list);
    }

    public function mechantOrder(Request $request){
        $params = $request -> post();
        $userName = isset($params['account']) ? $params['account'] : '';//充值账号
        $goods_id = isset($params['goodsId']) ? $params['goodsId'] : '';//商品id
        $goods_code = isset($params['goods_code']) ? $params['goods_code'] : '';//商品id
        $recharge_status = isset($params['recharge_status']) ? $params['recharge_status'] : '';//订单状态
        $start_time = $params['start_time'];//开始时间
        $end_time = $params['end_time'];//结束时间
        $Merchant_num = isset($params['order_sn']) ? $params['order_sn'] : '';//商户订单号
        $Merchant = isset($params['mecahnt_num']) ? $params['mecahnt_num'] : '';//商户号
        $types = isset($params['types']) ?  $params['types'] : 0;
        $condition = [];
        if(!empty($start_time) && !empty($end_time)){
            $condition[] = ['recharge_time','>=',$start_time];
            $condition[] = ['recharge_time','<=',$end_time];
        }
        if(!empty($Merchant)){
            $condition[] = ['merchant_number','=',$Merchant];
        }
        if(!empty($goods_id)){
            $condition[] = ['id','=',$goods_id];

        }
        if(!empty($goods_code)){
            $condition[] = ['goods_code','=',$goods_code];

        }
        if(!empty($Merchant_num)){
            $condition[] = ['order_sn','=',$Merchant_num];
        }
        if($recharge_status != null){
            $condition[] = ['recharge_status','=',$recharge_status];
        }
        if (!empty($userName)){
            $condition[] = ['account','=',$userName];
        }
        $condition[] = ['order_type','=',$types];
        $res = XnOrder::where($condition)
            ->orderBy('id','desc')
            ->get()
            ->toArray();
        if (!$res){
            return fails('查询失败');
        }else{
            if($res){
                foreach($res as $k => $v){
                    $res[$k]['goods_belong'] = InterfaceConfiguration::where('id',$v['goods_belong']) -> value('title');
                }
            }
            return successly($res);
        }
    }
}
